<template>
  <div class="login-container">

    <el-form ref="loginForm" :model="loginForm" class="login-form" :rules="rules" autocomplete="on" label-position="left">
      <div class="title-container">
        <h1 class="title_40">{{ $t('title') }}</h1>
      </div>

      <div class="title-container">
        <h3 class="title">{{ $t('login') }}</h3>
      </div>

      <el-form-item prop="username" class="el-form-item-class">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          class="el-input-class"
          :placeholder="$t('username')"
          name="username"
          type="text"
          tabindex="1"
          autocomplete="on"
        />
      </el-form-item>

      <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
        <el-form-item prop="password" class="el-form-item-class">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            class="el-input-class"
            :type="passwordType"
            :placeholder="$t('password')"
            name="password"
            tabindex="2"
            autocomplete="on"
            @keyup.native="checkCapslock"
            @blur="capsTooltip = false"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
      </el-tooltip>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">{{ $t('login') }}</el-button>

    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 1999-{{ currentYear }} Ammic All Rights Reserved.</span>
    </div>
    <ChangePassword ref="changePassword" />
  </div>
</template>

<script>
import store from '@/store';
import ChangePassword from '@/views/login/components/ChangePassword';
import targaVue from '@/main';
export default {
  name: 'Login',
  components: {
    ChangePassword
  },
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      options: [],
      currentYear: '',
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      redirect: undefined,
      otherQuery: {},
      rules: {
        username: [
          { required: true, message: this.$t('inputUserName'), trigger: 'blur' }
        ],
        password: [
          { required: true, message: this.$t('inputPassword'), trigger: 'blur' }
        ]
      }
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true
    }
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus();
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus();
    }

    var myDate = new Date();
    this.currentYear = myDate.getFullYear();
  },
  methods: {
    checkCapslock(e) {
      const { key } = e;
      this.capsTooltip = key && key.length === 1 && key >= 'A' && key <= 'Z';
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = '';
      } else {
        this.passwordType = 'password';
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch('user/login', this.loginForm)
            .then((resp) => {
              if (!resp.success) {
                this.targaMessageBox(this, resp.msg);
                store.dispatch('user/resetToken');
              } else {
                if (resp.code === 50009) {
                  this.$confirm(
                    targaVue.$t('passwordWillExpired'),
                    targaVue.$t('warning'),
                    {
                      confirmButtonText: targaVue.$t('modifyPassword'),
                      cancelButtonText: targaVue.$t('cancel'),
                      type: 'warning'
                    }
                  )
                    .then(() => {
                      this.$refs.changePassword.initial(
                        this.loginForm.username
                      );
                    })
                    .catch(() => {
                      this.$router.push({
                        path: this.redirect || '/',
                        query: this.otherQuery
                      });
                    });
                } else if (resp.code === 50010) {
                  this.$alert(
                    targaVue.$t('passwordIsInitial'),
                    targaVue.$t('warning'),
                    {
                      confirmButtonText: targaVue.$t('modifyPassword'),
                      type: 'warning'
                    }
                  ).then(() => {
                    this.$refs.changePassword.initial(this.loginForm.username);
                  });
                } else {
                  this.$router.push({
                    path: this.redirect || '/',
                    query: this.otherQuery
                  });
                }
              }
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    }
  }
};
</script>

<style lang="scss">

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

/* reset element-ui css */
.login-container {
  .el-input-class {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item-class {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>

$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {

  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }

    .title_40 {
      font-size: 40px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
